#coding=utf-8

def average(alist):
    '''算术平均值'''
    if type(alist[0]) == type(1):return sum(alist)/float(len(alist))
    return sum(alist)/len(alist)

def median(alist):
    '''中位数'''
    alist.sort()
    n = len(alist)
    
    if (n+1)%2 == 0:
        i = (n+1)/2 -1
        return alist[i]
    
    x = n/2
    y = x-1
    if type(alist[0]) == type(1):
        return (alist[x] + alist[y])/2.0
    return (alist[x] + alist[y])/2

def sma(alist, period):
    n = len(alist)
    if n < period:
        return [None for i in range(n)]
    
    r = [None for i in range(period-1)]
    for i in range(period-1, len(alist)):
        start = i - period + 1
        end = i + 1
        r.append(average(alist[start:end]))
        
    return r
